import axios from "axios";
import { Toast } from "zarm";

let MODE = import.meta.env.MODE

axios.defaults.baseURL = MODE = 'development' ? '/api' : ''
axios.defaults.withCredentials = true
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers['Authorization'] = `${localStorage.getItem('token') || null}`
axios.defaults.headers.post['Content-Type'] = 'application/json'

axios.interceptors.response.use(res => {
    if (res.data.code !== 200) {
        Toast.show(res.data.msg)
        if (res.data.code == 401) {
            window.location.href = '/login'
        }
        return Promise.reject(res)
    }
    return res.data
})

export default axios